package com.chushouya.order.service.common.impl;

import com.general.framework.core.lang.Dates;
import com.chushouya.order.dao.entity.StatManageDayEntity;
import com.chushouya.order.dao.repository.StatManageDayRepository;
import com.chushouya.order.dto.admin.stat.ManageDayStatQuery;
import com.chushouya.order.service.common.ManageDayStatTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class ManageDayStatTaskServiceImpl implements ManageDayStatTaskService {

    @Resource
    private StatManageDayRepository manageDayStatRepository;

    @Resource
    private ManageDayStatTaskService manageDayStatTaskService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void manageTodayStat(Date date) {
        Date statDate;
        if (Objects.isNull(date)) {
            statDate = Dates.getFirstTime(Dates.getTimeNow());
        } else {
            statDate = Dates.getFirstTime(date);
        }
        log.info("manageTodayStat,statDate:{}", statDate);
        this.manageDayStatHandler(statDate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void manageYesterdayStat(Date date) {
        Date statDate;
        if (Objects.isNull(date)) {
            statDate = Dates.getFirstTime(Dates.addDays(Dates.getTimeNow(), -1));
        } else {
            statDate = Dates.getFirstTime(Dates.addDays(date, -1));
        }
        this.manageDayStatHandler(statDate);
    }

    private void manageDayStatHandler(Date statDate) {
        final Date startTime = Dates.getFirstTime(statDate);
        final Date endTime = Dates.getLastTime(statDate);
        final Date timeNow = Dates.getTimeNow();

        try {
            manageDayStatTaskService.statOne(statDate, startTime, endTime, timeNow);
        } catch (Exception e) {
            log.error("统计运营数据异常,statDate:{},{}",statDate, e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void statOne(Date statDate, Date startTime, Date endTime, Date timeNow) {
        try {
            ManageDayStatQuery query = new ManageDayStatQuery();
            query.setStatDate(statDate);
            StatManageDayEntity orderDayStat = manageDayStatRepository.selectOne(query);
            StatManageDayEntity stat = new StatManageDayEntity();
            stat.setStatDate(statDate);
            if (Objects.isNull(orderDayStat)) {
                stat.setStatDate(statDate);
                stat.setCreateTime(timeNow);
                stat.setLastStatTime(timeNow);
                this.setStatField(startTime, endTime,stat);
            } else {
                stat.setStatId(orderDayStat.getStatId());
                stat.setLastStatTime(timeNow);
                this.setStatField(startTime, endTime, stat);
            }
            if (Objects.isNull(orderDayStat)) {
                manageDayStatRepository.insertSelective(stat);
            } else {
                manageDayStatRepository.updateByPrimaryKeySelective(stat);
            }
            log.info("===>统计运营数据完成,statDate:{}", statDate);
        } catch (Exception e) {
            log.error("统计运营数据错误,statDate:{}", statDate, e);
        }
    }

    private void setStatField(Date startTime, Date endTime, StatManageDayEntity stat) {
        log.info("===>统计运营数据,startTime:{},endTime:{}", startTime, endTime);
        stat.setOrderCount(manageDayStatRepository.selectOrderCount(startTime, endTime));
        stat.setUserCount(manageDayStatRepository.selectUserCount(startTime, endTime));
        stat.setVisitCount(manageDayStatRepository.selectVisitCount(startTime, endTime));
        stat.setEvaluateCount(manageDayStatRepository.selectEvaluateCount(startTime, endTime));
        stat.setProductCount(manageDayStatRepository.selectProductCount());
    }

}
